import {cancelCollectCommodity, collectCommodity} from "../../../../../utils/request";

Component({
  externalClasses: ['wr-sold-out', 'wr-class'],

  options: { multipleSlots: true },

  properties: {
    commodityId: {
      type: Number,
      required: true
    },
    collected: {
      // 当前用户是否收藏
      type: Boolean,
      value: false,
    },
    soldout: {
      // 商品是否下架
      type: Boolean,
      value: false,
    },
    jumpArray: {
      type: Array,
      value: [],
    },
    isStock: {
      type: Boolean,
      value: true,
    }, // 是否有库存
    isSlotButton: {
      type: Boolean,
      value: false,
    }, // 是否开启按钮插槽
    shopCartNum: {
      type: Number, // 购物车气泡数量
    },
    buttonType: {
      type: Number,
      value: 0,
    },
    minDiscountPrice: {
      type: String,
      value: '',
    },
    minSalePrice: {
      type: String,
      value: '',
    },
    housekeeper: {
      type: Object,
      value: {},
    },
  },

  data: {
    fillPrice: false,
    collected: false
  },

  methods: {
    onLoad() {
      this.setData({
        collected: this.properties.collected
      })
    },

    toAddCart() {
      wx.navigateBack({
        delta: 1
      });    
    },

    toBuyNow(e) {
      // const { isStock } = this.properties;
      // if (!isStock) return;
      this.triggerEvent('toBuyNow', e);
      wx.navigateTo({
        url: '/pages/goods/create-order/index?housekeeperId=' + this.properties.commodityId,
      })
    },

    jumpToConsult(e) {
      wx.navigateTo({
        url: '/pages/chat/chat/chat?companyName=' + this.properties.housekeeper.companyName,
      })
    },

    async collect(e) {
      console.log("收藏");
      const c = this.data.collected;
      this.setData({
        collected: !c
      })
      if (c) {
        await collectCommodity(this.properties.commodityId);
      } else {
        await cancelCollectCommodity(this.properties.commodityId);
      }
    },
  },
});
